package controllers;

import helpers.UrlHelper;

import java.io.IOException;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.Connection.Response;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class CheckHtmlTagCounterServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		TreeMap<String, String> check = new TreeMap<String, String>();
		String name = (String) request.getParameter("domain");
		TreeMap<String, String> tags = new TreeMap<String, String>();
		
		if (name != null && !UrlHelper.checkDomain(name)) {
			check.put("name", "Please input a valid domain.");
		} else if (name != null) {
			name = UrlHelper.getMainDomain(name);
		} 
		
		
		if (name != null && check.size() == 0) {
			String url = "http://" + name;
			try {
				Connection conn = Jsoup.connect(url);
				Document doc = conn
						.header("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
						.header("accept-Language", "en-us,en;q=0.5")
						.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1")
						.followRedirects(true)
						.timeout(3000)
						.get();
				
				Elements h1 = doc.select("H1");
	            Elements h2 = doc.select("H2");
	            Elements h3 = doc.select("H3");
	            Elements h4 = doc.select("H4");
	            Elements h5 = doc.select("H5");
	            Elements h6 = doc.select("H6");
	            Elements img = doc.select("img");
	            Elements div = doc.select("div");
	            Elements span = doc.select("span");
	            Elements script = doc.select("script");
	            Elements table = doc.select("table");
	            Elements tr = doc.select("tr");
	            Elements th = doc.select("th");
	            Elements td = doc.select("td");
	            Elements link = doc.select("link");
	            Elements object = doc.select("object");
	            Elements param = doc.select("param");
	            Elements embed = doc.select("embed");
	            Elements meta = doc.select("meta");
	            Elements style = doc.select("style");
	            Elements form = doc.select("form");
	            Elements input = doc.select("input");
	            Elements select = doc.select("select");
	            Elements option = doc.select("option");
	            
	            tags.put("H1", Integer.toString(h1.size()));
	            tags.put("H2", Integer.toString(h2.size()));
	            tags.put("H3", Integer.toString(h3.size()));
	            tags.put("H4", Integer.toString(h4.size()));
	            tags.put("H5", Integer.toString(h5.size()));
	            tags.put("H6", Integer.toString(h6.size()));
	            tags.put("Img", Integer.toString(img.size()));
	            tags.put("Div", Integer.toString(div.size()));
	            tags.put("Span", Integer.toString(span.size()));
	            tags.put("Script", Integer.toString(script.size()));
	            tags.put("Table", Integer.toString(table.size()));
	            tags.put("Tr", Integer.toString(tr.size()));
	            tags.put("Th", Integer.toString(th.size()));
	            tags.put("Td", Integer.toString(td.size()));
	            tags.put("Link", Integer.toString(link.size()));
	            tags.put("Object", Integer.toString(object.size()));
	            tags.put("Param", Integer.toString(param.size()));
	            tags.put("Embed", Integer.toString(embed.size()));
	            tags.put("Meta", Integer.toString(meta.size()));
	            tags.put("Style", Integer.toString(style.size()));
	            tags.put("Form", Integer.toString(form.size()));
	            tags.put("Input", Integer.toString(input.size()));
	            tags.put("Select", Integer.toString(select.size()));
	            tags.put("Option", Integer.toString(option.size()));
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		
		//Cache header
		response.addHeader("Cache-Control", "public, max-age=60000");
		
		request.setAttribute("check", check);
		request.setAttribute("name", name);
		request.setAttribute("tags", tags);
		try {
			request.getRequestDispatcher("/pages/check_html_tag_counter.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
}
